package com.netease.xone.itemview;

import java.util.ArrayList;
import java.util.List;

import protocol.meta.AppAudio;
import protocol.meta.AppBlock;
import protocol.meta.AppImage;
import protocol.meta.AppInfo;
import protocol.meta.AppInfoDetail;
import protocol.meta.AppVideo;
import protocol.meta.AppWare;
import Utils.StackTraceUtil;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.activity.ActivityMovieView;
import com.netease.xone.widget.LoadingImageView;


public class RenderInfoOfficialBase implements IRenderInfoBase{
    private static final String TAG = RenderInfoOfficialBase.class.getSimpleName();
    
    private int mWidth;
    private int mRightMargin;
    private int mTopMargin;
    
    private LinearLayout mBlockContainer;
    private LoadingImageView mImage;
    private ImageView mVideo;
    private TextView  mTitle;
    private TextView  mText;
    
    public RenderInfoOfficialBase(View v) {
        mBlockContainer = (LinearLayout)v.findViewById(R.id.info_block_container);
        mImage = (LoadingImageView)v.findViewById(R.id.image);
        mVideo = (ImageView)v.findViewById(R.id.video);
        mTitle = (TextView)v.findViewById(R.id.title);
        mText = (TextView)v.findViewById(R.id.text);
        
        mWidth = v.getContext().getResources().getDisplayMetrics().widthPixels;
        
        mRightMargin = v.getContext().getResources().getDimensionPixelSize(R.dimen.info_size_10);
        mTopMargin = v.getContext().getResources().getDimensionPixelSize(R.dimen.info_size_5);
        
        mVideo.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                Log.d(TAG, StackTraceUtil.getMethodName());
                String value = null;
                AppVideo video = null;
                if (null != v.getTag() && v.getTag() instanceof AppVideo) {
                    video = (AppVideo)v.getTag();
                    value = video.getMediaUrl();
                    ActivityMovieView.playVideo(v.getContext(), value);
                }
            }
        });
    }

    @Override
    public void renderView(Context context, AppInfo appInfo) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        
        String value = null;
        mImage.setVisibility(View.GONE);
        mVideo.setVisibility(View.GONE);
        value = appInfo.getTitle();
        if (TextUtils.isEmpty(value)) {
            mTitle.setVisibility(View.GONE);
        } else {
            mTitle.setVisibility(View.VISIBLE);
        }
        mTitle.setText(value);
        
        value = appInfo.getDigest();
        if (TextUtils.isEmpty(value)) {
            mText.setVisibility(View.GONE);
        } else {
            mText.setVisibility(View.VISIBLE);
        }
        mText.setText(value);
        
        mBlockContainer.setVisibility(View.GONE);
        IRenderInfoBase iRenderInfoBase = null;
        View v = null;
        switch (appInfo.getInfoType()) {
            case AppInfo.TYPE_PHOTO: {
                List<AppImage> list = appInfo.getImages();
                if (null != list && 0 < list.size()) {
                    value = list.get(0).getUrl();
                    if (!TextUtils.isEmpty(value)) {
                        mImage.setVisibility(View.VISIBLE);
                        mImage.setServerClipSize(mWidth, 0);
                        mImage.setLoadingImage(value);
                    }
                }
                break;
            }
            case AppInfo.TYPE_AUDIO:
                if (null != mBlockContainer.getTag() && mBlockContainer.getTag() instanceof RenderInfoAudio) {
                    iRenderInfoBase = (IRenderInfoBase)mBlockContainer.getTag();
                } else {
                    v = View.inflate(context, R.layout.item_view_info_audio, null);
                    iRenderInfoBase = new RenderInfoAudio(v);
                }
                if (null != appInfo.getAudios() && appInfo.getAudios().size() > 0) {
                    mBlockContainer.setVisibility(View.VISIBLE);
                }
                break;
            case AppInfo.TYPE_RADIO:
                if (null != mBlockContainer.getTag() && mBlockContainer.getTag() instanceof RenderInfoRadio) {
                    iRenderInfoBase = (IRenderInfoBase)mBlockContainer.getTag();
                } else {
                    v = View.inflate(context, R.layout.item_view_info_radio, null);
                    iRenderInfoBase = new RenderInfoRadio(v);
                }
                if (null != appInfo.getRadios() && appInfo.getRadios().size() > 0) {
                    mBlockContainer.setVisibility(View.VISIBLE);
                }
                break;
            case AppInfo.TYPE_VIDEO:{
                List<AppVideo> list = appInfo.getVideos();
                if (null != list && 0 < list.size()) {
                    value = list.get(0).getImageUrl();
                    if (!TextUtils.isEmpty(value)) {
                        mImage.setVisibility(View.VISIBLE);
                        mVideo.setVisibility(View.VISIBLE);
                        mImage.setServerClipSize(mWidth, 0);
                        mImage.setLoadingImage(value);
                        mVideo.setTag(list.get(0));
                    }
                }
                break;
            }
            case AppInfo.TYPE_IMAGES:
                if (null != mBlockContainer.getTag() && mBlockContainer.getTag() instanceof RenderInfoImages) {
                    iRenderInfoBase = (IRenderInfoBase)mBlockContainer.getTag();
                } else {
                    v = View.inflate(context, R.layout.item_view_info_images, null);
                    iRenderInfoBase = new RenderInfoImages(v);
                }
                if (null != appInfo.getGallery() && appInfo.getGallery().size() > 0) {
                    mBlockContainer.setVisibility(View.VISIBLE);
                }
                break;
            case AppInfo.TYPE_SPECIAL:
                if (null != appInfo.getSpecial()) {
                    if (!TextUtils.isEmpty(appInfo.getSpecial().getImageUrl())) {
                        mImage.setVisibility(View.VISIBLE);
                        mImage.setLoadingImage(appInfo.getSpecial().getImageUrl());
                    }
                }
                if (null != mBlockContainer.getTag() && mBlockContainer.getTag() instanceof RenderInfoSpecial) {
                    iRenderInfoBase = (IRenderInfoBase)mBlockContainer.getTag();
                } else {
                    v = View.inflate(context, R.layout.item_view_info_special, null);
                    iRenderInfoBase = new RenderInfoSpecial(v);
                }
                if (null != appInfo.getSpecial()) {
                    mBlockContainer.setVisibility(View.VISIBLE);
                }
                break;
            case AppInfo.TYPE_VOTE:
                if (null != mBlockContainer.getTag() && mBlockContainer.getTag() instanceof RenderInfoVote) {
                    iRenderInfoBase = (IRenderInfoBase)mBlockContainer.getTag();
                } else {
                    v = View.inflate(context, R.layout.item_view_info_vote, null);
                    iRenderInfoBase = new RenderInfoVote(v);
                }
                if (null != appInfo.getVote()) {
                    mBlockContainer.setVisibility(View.VISIBLE);
                }
                break;
            case AppInfo.TYPE_PK:
                if (null != mBlockContainer.getTag() && mBlockContainer.getTag() instanceof RenderInfoPK) {
                    iRenderInfoBase = (IRenderInfoBase)mBlockContainer.getTag();
                } else {
                    if (null != appInfo.getPk() && appInfo.getPk().getFinished()) {
                        v = View.inflate(context, R.layout.item_view_info_pk_finish, null);
                    } else {
                        v = View.inflate(context, R.layout.item_view_info_pk, null);
                    }
                    iRenderInfoBase = new RenderInfoPK(v);
                }
                if (null != appInfo.getPk()) {
                    mBlockContainer.setVisibility(View.VISIBLE);
                }
                break;
            default:
                break;
        }
        
        RenderInfoBase.renderView(iRenderInfoBase, context, appInfo);
        if (null != v) {
            mBlockContainer.removeAllViews();
            mBlockContainer.addView(v);
        }
        mBlockContainer.setTag(iRenderInfoBase);
    }

    @Override
    public void renderView(Context context, AppInfoDetail appInfoDetail) {
        String value = null;
        mImage.setVisibility(View.GONE);
        mVideo.setVisibility(View.GONE);
        mTitle.setVisibility(View.GONE);
        mText.setVisibility(View.GONE);
        
        value = appInfoDetail.getTitle();
        if (!TextUtils.isEmpty(value)) {
            mTitle.setVisibility(View.VISIBLE);
            mTitle.setText(value);
        }
        
        switch (appInfoDetail.getInfoType()) {
            case AppInfo.TYPE_PHOTO: {
                value = appInfoDetail.getImageUrl();
                if (!TextUtils.isEmpty(value)) {
                    mImage.setVisibility(View.VISIBLE);
                    mImage.setServerClipSize(mWidth, 0);
                    mImage.setLoadingImage(value);
                }
                break;
            }
            case AppInfo.TYPE_IMAGES:{
                // 图集模式隐藏
                mTitle.setVisibility(View.GONE);
                break;
            }
            case AppInfo.TYPE_SPECIAL:{
                // 专题模式需要隐藏
                mTitle.setVisibility(View.GONE);
                mText.setVisibility(View.GONE);
                break;
            }
        }
        
        View v = null;
        
        List<AppBlock> list = appInfoDetail.getbList();
        View[] childViews = new View[list.size()*2];
        int index = 0;
        // 更具不同的子类生成不同的子view加入到主容器中
        for (AppBlock block : list) {
            switch (block.getResType()) {
                case AppInfo.TYPE_TEXT: {
                    if (!TextUtils.isEmpty(block.getText())) {
                        v = View.inflate(context, R.layout.item_view_detail_text, null);
                        RenderDetailText renderDetailText = new RenderDetailText(v);
                        renderDetailText.renderView(context, block.getText());
                        childViews[index] = v;
                    }
                    break;
                }
                case AppInfo.TYPE_PHOTO: {
                    if (!TextUtils.isEmpty(block.getText())) {
                        v = View.inflate(context, R.layout.item_view_detail_text, null);
                        RenderDetailText renderDetailText = new RenderDetailText(v);
                        renderDetailText.renderView(context, block.getText());
                        childViews[index] = v;
                        index++;
                    }
                    v = View.inflate(context, R.layout.item_view_detail_image, null);
                    RenderDetailImage renderDetailImage = new RenderDetailImage(v);
                    renderDetailImage.renderView(context, block.getImage());
                    childViews[index] = v;
                    break;
                }
                case AppInfo.TYPE_AUDIO:{
                    if (!TextUtils.isEmpty(block.getText())) {
                        v = View.inflate(context, R.layout.item_view_detail_text, null);
                        RenderDetailText renderDetailText = new RenderDetailText(v);
                        renderDetailText.renderView(context, block.getText());
                        childViews[index] = v;
                        index++;
                    }
                    v = View.inflate(context, R.layout.item_view_info_audio, null);
                    RenderInfoAudio renderInfoAudio = new RenderInfoAudio(v);
                    List<AppAudio> audioList = new ArrayList<AppAudio>();
                    audioList.add(block.getAudio());
                    renderInfoAudio.renderView(context, audioList);
                    childViews[index] = v;
                    break;
                }
                case AppInfo.TYPE_RADIO:{
                    
                    break;
                }
                case AppInfo.TYPE_VIDEO:{
                    if (!TextUtils.isEmpty(block.getText())) {
                        v = View.inflate(context, R.layout.item_view_detail_text, null);
                        RenderDetailText renderDetailText = new RenderDetailText(v);
                        renderDetailText.renderView(context, block.getText());
                        childViews[index] = v;
                        index++;
                    }
                    
                    v = View.inflate(context, R.layout.item_view_detail_video, null);
                    RenderDetailVideo renderDetailVideo = new RenderDetailVideo(v);
                    renderDetailVideo.renderView(context, block.getVideo());
                    childViews[index] = v;
                    break;
                }
                case AppInfo.TYPE_IMAGES:{
                    v = View.inflate(context, R.layout.item_view_detail_gallery, null);
                    RenderDetailGallery renderDetailGallery = new RenderDetailGallery(v);
                    renderDetailGallery.renderView(context, block.getGallery(), appInfoDetail.getTitle());
                    childViews[index] = v;
                    break;
                }
                case AppInfo.TYPE_SPECIAL:{
                    if (!TextUtils.isEmpty(block.getText())) {
                        v = View.inflate(context, R.layout.item_view_detail_text, null);
                        RenderDetailText renderDetailText = new RenderDetailText(v);
                        renderDetailText.renderView(context, block.getText());
                        childViews[index] = v;
                        index++;
                    }
                    
                    v = View.inflate(context, R.layout.item_view_info_special, null);
                    RenderInfoSpecial renderInfoSpecial = new RenderInfoSpecial(v);
                    renderInfoSpecial.renderView(context, block.getSpecial(), appInfoDetail.getInfoId(), true, true);
                    childViews[index] = v;
                    break;
                }
                case AppInfo.TYPE_VOTE:{
                    if (!TextUtils.isEmpty(block.getText())) {
                        v = View.inflate(context, R.layout.item_view_detail_text, null);
                        RenderDetailText renderDetailText = new RenderDetailText(v);
                        renderDetailText.renderView(context, block.getText());
                        childViews[index] = v;
                        index++;
                    }
                    
                    v = View.inflate(context, R.layout.item_view_info_vote, null);
                    RenderInfoVote renderInfoVote = new RenderInfoVote(v);
                    renderInfoVote.renderView(context, block.getVote());
                    childViews[index] = v;
                    break;
                }
                case AppInfo.TYPE_PK:{
                    if (!TextUtils.isEmpty(block.getText())) {
                        v = View.inflate(context, R.layout.item_view_detail_text, null);
                        RenderDetailText renderDetailText = new RenderDetailText(v);
                        renderDetailText.renderView(context, block.getText());
                        childViews[index] = v;
                        index++;
                    }
                    
                    if (null != block.getPk() && block.getPk().getFinished()) {
                        v = View.inflate(context, R.layout.item_view_info_pk_finish, null);
                    } else {
                        v = View.inflate(context, R.layout.item_view_info_pk, null);
                    }
                    RenderInfoPK renderInfoPK = new RenderInfoPK(v);
                    renderInfoPK.renderView(context, block.getPk());
                    childViews[index] = v;
                    break;
                }
            }
            index++;
        }
        
        AppWare appWare = appInfoDetail.getWare();
        if (null != appWare) {
            v = View.inflate(context, R.layout.item_view_detail_ware, null);
            RenderDetailWare renderDetailWare = new RenderDetailWare(v);
            renderDetailWare.renderView(context, appWare);
            childViews[index] = v;
        }
        
        mBlockContainer.removeAllViews();
        
        LinearLayout.LayoutParams params;
        for (View child : childViews) {
            if (null != child) {
                params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                params.leftMargin = mRightMargin;
                params.rightMargin = mRightMargin;
                params.topMargin = mTopMargin;
                params.bottomMargin = mTopMargin;
                mBlockContainer.addView(child, params);
            }
        }
        
        mBlockContainer.setVisibility(View.VISIBLE);
    }
}
